package com.qianli.user.application;

import com.fqgj.common.api.Response;
import com.qianli.user.domain.enums.UserAppInfoAddType;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.facatory.UserBehaviorFactory;
import com.qianli.user.domain.model.UserBehaviorModel;
import com.qianli.user.domain.model.behavior.*;
import com.qianli.user.ro.app.AppInfoRO;
import com.qianli.user.ro.behavior.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * name:行为信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/2 19:25
 * version: v1.0.0
 **/
@Service
public class UserBehaviorApplication extends UserAbstractApplication{

    @Autowired
    private UserBehaviorFactory userBehaviorFactory;

    public UserBehaviorInfoRO load(String userCode){
        //加载模型
        UserBehaviorModel model = new UserBehaviorModel();
        model.setUserCode(userCode);
        userBehaviorFactory.load(model);

        UserBehaviorInfoRO userBehaviorInfoRO = convertModelToRO(model);

        return userBehaviorInfoRO;
    }

    /**
     * model to ro
     * @param model
     * @return
     */
    private UserBehaviorInfoRO convertModelToRO(UserBehaviorModel model) {
        UserBehaviorInfoRO behaviorInfoRO = new UserBehaviorInfoRO();
        //设备信息
        if(null != model.getUserBehaviorDevice()){
            UserBehaviorDeviceRO userBehaviorDeviceRO = new UserBehaviorDeviceRO();
            BeanUtils.copyProperties(model.getUserBehaviorDevice(),userBehaviorDeviceRO);
            behaviorInfoRO.setUserBehaviorDevice(userBehaviorDeviceRO);
        }
        //gps
        if(null != model.getUserBehaviorGps()){
            UserBehaviorGpsRO userBehaviorGpsRO = new UserBehaviorGpsRO();
            BeanUtils.copyProperties(model.getUserBehaviorGps(),userBehaviorGpsRO);
            behaviorInfoRO.setUserBehaviorGps(userBehaviorGpsRO);
        }
        //数美
        if(null != model.getUserBehaviorShumei()){
            UserBehaviorDeviceShumeiRO shumeiRO = new UserBehaviorDeviceShumeiRO();
            BeanUtils.copyProperties(model.getUserBehaviorShumei(),shumeiRO);
            behaviorInfoRO.setUserBehaviorDeviceShumei(shumeiRO);
        }
        //wifi信息
        if(null != model.getUserBehaviorWifi()){
            UserBehaviorWifiRO wifiRO = new UserBehaviorWifiRO();
            BeanUtils.copyProperties(model.getUserBehaviorWifi(),wifiRO);
            behaviorInfoRO.setUserBehaviorWifi(wifiRO);
        }
        return behaviorInfoRO;
    }

    /**
     * 添加设备相关信息
     * @param userCode
     * @param appInfo
     * @param type
     */
    public void addUserAppInfo(String userCode, AppInfoRO appInfo, UserAppInfoAddType type) {
        UserBehaviorModel userBehaviorModel = new UserBehaviorModel();
        //设备行为信息
        UserBehaviorAppInfo userAppInfo = new UserBehaviorAppInfo();
        userAppInfo.setUserCode(userCode);
        userAppInfo.setPlat(appInfo.getVersion().getPlat());
        userAppInfo.setVersion(appInfo.getVersion().getVersion());
        userAppInfo.setVersionCode(appInfo.getVersion().getVersionCode());
        userAppInfo.setPushType(appInfo.getPush().getType());
        userAppInfo.setPushId(appInfo.getPush().getPushId());
        userAppInfo.setChannel(appInfo.getChannel().getChannel());
        userAppInfo.setGuestId(appInfo.getGuestId());
        userAppInfo.setChannelId(appInfo.getChannel().getChannelId());
        userAppInfo.setAppCode(appInfo.getAppCode());
        userAppInfo.setIp(appInfo.getIp());
        userAppInfo.setFrom(appInfo.getFrom());
        userAppInfo.setPackageName(appInfo.getPackageName());
        userBehaviorModel.setAppInfo(userAppInfo);
        //设备信息
        UserBehaviorDevice userBehaviorDevice = new UserBehaviorDevice();
        userBehaviorDevice.setUserCode(userCode);
        userBehaviorDevice.setGuestId(appInfo.getGuestId());
        userBehaviorModel.setUserBehaviorDevice(userBehaviorDevice);
        if(isNew(userBehaviorModel)){
            userBehaviorDevice.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        }
        if(UserAppInfoAddType.LOGIN.equals(type)){
            userBehaviorModel.getAppInfo().setItemStatus(UserModelItemStatusEnum.UPDATE.getType());
        }else{
            userBehaviorModel.getAppInfo().setItemStatus(UserModelItemStatusEnum.ADD.getType());
        }

        userBehaviorFactory.store(userBehaviorModel);

    }

    /**
     * 保存行为信息
     * @param userBehaviorInfo
     * @return
     */
    public Response<Boolean> saveUserBehaviorInfo(UserBehaviorInfoRO userBehaviorInfo) {
        //加载模型
        UserBehaviorModel oldUm = new UserBehaviorModel();
        oldUm.setUserCode(userBehaviorInfo.getUserCode());
        userBehaviorFactory.load(oldUm);

        UserBehaviorModel newUm = new UserBehaviorModel();
        newUm = converROToModel(userBehaviorInfo,newUm,oldUm);

        userBehaviorFactory.store(newUm);
        return Response.ok(true);

    }

    private UserBehaviorModel converROToModel(UserBehaviorInfoRO userBehaviorInfo, UserBehaviorModel newUm,UserBehaviorModel oldUm) {
        String userCode = userBehaviorInfo.getUserCode();
        //设备
        if(null != userBehaviorInfo.getUserBehaviorDevice()){
            UserBehaviorDevice userBehaviorDevice = new UserBehaviorDevice();
            userBehaviorDevice.setUserCode(userCode);
            userBehaviorDevice.setGuestId(userBehaviorInfo.getUserBehaviorDevice().getGuestId());
            userBehaviorDevice.setUserCode(userBehaviorInfo.getUserBehaviorDevice().getUserCode());
            newUm.setUserBehaviorDevice(userBehaviorDevice);
            if(isNew(newUm)){
                userBehaviorDevice.setItemStatus(UserModelItemStatusEnum.ADD.getType());
            }

        }
        //数美
        if(null != userBehaviorInfo.getUserBehaviorDeviceShumei()){
            UserBehaviorShumei userBehaviorShumei = new UserBehaviorShumei();
            userBehaviorShumei.setUserCode(userCode);
            userBehaviorShumei.setFingerInfoUrl(userBehaviorInfo.getUserBehaviorDeviceShumei().getFingerInfoUrl());
            userBehaviorShumei.setPlatForm(userBehaviorInfo.getUserBehaviorDeviceShumei().getPlatForm());
            userBehaviorShumei.setSmid(userBehaviorInfo.getUserBehaviorDeviceShumei().getSmid());
            newUm.setUserBehaviorShumei(userBehaviorShumei);
            userBehaviorShumei.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        }
        //gps
        if(null != userBehaviorInfo.getUserBehaviorGps()){
            UserBehaviorGps ug = new UserBehaviorGps();
            ug.setUserCode(userCode);
            ug.setLat(userBehaviorInfo.getUserBehaviorGps().getLat());
            ug.setLng(userBehaviorInfo.getUserBehaviorGps().getLng());
            ug.setGmtCreate(new Date());
            newUm.setUserBehaviorGps(ug);
            ug.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        }
        //wifi
        if(null != userBehaviorInfo.getUserBehaviorWifi()){
            UserBehaviorWifi uw = new UserBehaviorWifi();
            uw.setUserCode(userCode);
            uw.setBssid(userBehaviorInfo.getUserBehaviorWifi().getBssid());
            uw.setSsid(userBehaviorInfo.getUserBehaviorWifi().getSsid());
            uw.setType(userBehaviorInfo.getUserBehaviorWifi().getType());
            newUm.setUserBehaviorWifi(uw);
            uw.setItemStatus(UserModelItemStatusEnum.ADD.getType());
        }
        return newUm;
    }

    /**
     * 是否是新信息
     * @param newUm
     * @return
     */
    public boolean isNew(UserBehaviorModel newUm) {

        return userBehaviorFactory.isNew(newUm);
    }

    /**
     * 保存行为信息-- 设备
     * @param userBehaviorDevice
     * @return
     */
    public Response<Boolean> saveUserBehaviorDevice(UserBehaviorDeviceRO userBehaviorDevice) {
        //加载行为信息
        UserBehaviorModel oldUm = new UserBehaviorModel();
        oldUm.setUserCode(userBehaviorDevice.getUserCode());
        userBehaviorFactory.load(oldUm);
        //对比行为信息模型
        UserBehaviorModel newUm = new UserBehaviorModel();
        UserBehaviorInfoRO userBehaviorInfo = new UserBehaviorInfoRO();
        userBehaviorInfo.setUserBehaviorDevice(userBehaviorDevice);
        converROToModel(userBehaviorInfo,newUm,oldUm);

        //更新行为信息
        userBehaviorFactory.store(newUm);
        return Response.ok(true);
    }

    /**
     * 保存行为信息--数美
     * @param userBehaviorDeviceShumei
     * @return
     */
    public Response<Boolean> saveUserBehaviorDeviceShumei(UserBehaviorDeviceShumeiRO userBehaviorDeviceShumei) {

        //加载行为信息
        UserBehaviorModel oldUm = new UserBehaviorModel();
        oldUm.setUserCode(userBehaviorDeviceShumei.getUserCode());
        userBehaviorFactory.load(oldUm);
        //对比数美信息
        UserBehaviorModel newUm = new UserBehaviorModel();
        UserBehaviorInfoRO userBehaviorInfo = new UserBehaviorInfoRO();
        userBehaviorInfo.setUserBehaviorDeviceShumei(userBehaviorDeviceShumei);
        converROToModel(userBehaviorInfo,newUm,oldUm);
        //更新行为信息
        userBehaviorFactory.store(newUm);
        return Response.ok(true);

    }

    /**
     * 保存行为信息gps
     * @param userBehaviorGps
     * @return
     */
    public Response<Boolean> saveUserBehaviorGps(UserBehaviorGpsRO userBehaviorGps) {

        //加载行为信息
        UserBehaviorModel oldUm = new UserBehaviorModel();
        oldUm.setUserCode(userBehaviorGps.getUserCode());
        userBehaviorFactory.load(oldUm);
        //对比gps
        UserBehaviorModel newUm = new UserBehaviorModel();
        UserBehaviorInfoRO userBehaviorInfo = new UserBehaviorInfoRO();
        userBehaviorInfo.setUserBehaviorGps(userBehaviorGps);
        converROToModel(userBehaviorInfo,newUm,oldUm);
        //更新行为信息
        userBehaviorFactory.store(newUm);
        return Response.ok(true);

    }

    /**
     * 保存行为信息--wifi
     * @param userBehaviorWifi
     * @return
     */
    public Response<Boolean> saveUserBehaviorWifi(UserBehaviorWifiRO userBehaviorWifi) {

        //加载行为信息
        UserBehaviorModel oldUm = new UserBehaviorModel();
        oldUm.setUserCode(userBehaviorWifi.getUserCode());
        userBehaviorFactory.load(oldUm);
        //对比wifi
        UserBehaviorModel newUm = new UserBehaviorModel();
        UserBehaviorInfoRO userBehaviorInfo = new UserBehaviorInfoRO();
        userBehaviorInfo.setUserBehaviorWifi(userBehaviorWifi);
        converROToModel(userBehaviorInfo,newUm,oldUm);
        //更新行为信息
        userBehaviorFactory.store(newUm);
        return Response.ok(true);

    }

    /**
     * 查询最新登录信息
     * @param userCode
     * @return
     */
    public UserBehaviorLoginRO queryUserLatestBehaviorLogin(String userCode) {
        UserBehaviorLoginRO loginRO = userBehaviorFactory.queryUserLatestBehaviorLogin(userCode);
        return loginRO;
    }

    /**
     * 查询最新注册信息
     * @param userCode
     * @return
     */
    public UserBehaviorRegisterRO getUserBehaviorRegister(String userCode) {
        UserBehaviorRegisterRO registerRO = userBehaviorFactory.getUserBehaviorRegister(userCode);
        return registerRO;
    }
}
